<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Short description for file
 *
 * Long description for file (if any)...
 *
 * PHP version 5
 *
 * Copyright 2008 The Picnic PHP Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @category      Framework
 * @package       Pfw
 * @author        Sean Sitter <sean@picnicphp.com>
 * @copyright     2008 The Picnic PHP Framework
 * @license       http://www.apache.org/licenses/LICENSE-2.0
 * @link          http://www.picnicphp.com
 * @since         0.10
 */

class Pfw_Config
{
    private static $config = null;

    public static function init()
    {
        self::_initDefaultConfig();
        self::_initEnvConfig();
    }
    
    public static function initForEnv($env)
    {
        self::_initDefaultConfig();
        self::_initEnvConfig($env);
    }


    public static function appendConfigFile($file)
    {
        if (is_null(self::$config)) {
            self::$config = array();
        }

        $new_config = include($file);
        self::$config = array_merge_recursive_replace(self::$config, $new_config);
    }


    public static function appendConfig($config)
    {
        if (is_null(self::$config)) {
            self::$config = array();
        }

        self::$config = array_merge_recursive_replace(self::$config, $config);
    }


    public static function setConfig($config)
    {
        if (!is_array($config)) {
            throw new Pfw_Exception("Config must be array");
        }
        self::$config = $config;
    }


    public static function reset(){
        self::$config = null;
    }


    public static function _initDefaultConfig()
    {
        if (is_null(self::$config)) {
            self::$config = array();
        }

        global $_PATHS;
        $default_config_file = $_PATHS['conf'].DIRECTORY_SEPARATOR."config.default.php";
        $new_config = include($default_config_file);
        self::$config = array_merge_recursive_replace(self::$config, $new_config);
    }


    public static function _initEnvConfig($env = null)
    {
        if (is_null(self::$config)) {
            self::$config = array();
        }

        global $_PATHS, $_ENVIRONMENT;
        $env = is_null($env) ? $_ENVIRONMENT : $env;
        if (isset($env)) {
            $env_config_file = $_PATHS['conf'].DIRECTORY_SEPARATOR."config.{$env}.php";
            $new_config = include($env_config_file);
            if (!empty($new_config)) {
                self::$config = array_merge_recursive_replace(self::$config, $new_config);
            }
        }
    }


    public static function getConfig()
    {
        if (is_null(self::$config)) {
            self::$config = array();
        }
        return self::$config;
    }


    public static function get($name)
    {
        $config = self::getConfig();
        if (!isset($config[$name])) {
            return null;
        }
        return $config[$name];
    }


    public static function set($name, $value)
    {
        self::$config[$name] = $value;
    }
}
